package web.mb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
import metier.entities.Transport;
import metier.entities.TransportCat;
import metier.sess.ITransportCatLocal;
import metier.sess.ITransportLocal;

@ManagedBean
@RequestScoped
public class TransportCatMB {
	@EJB
	private ITransportCatLocal metier;
	@EJB 
	private ITransportLocal metiert;
	private String destination="D:\\fileupload\\";

	private Integer Id;
	private Integer IdT;
	private TransportCat tr=new TransportCat();
	private UploadedFile file;
	
	
	public Integer getId() {
		return Id;
	}
	
	public void setId(Integer id) {
		Id = id;
	}
	
	public TransportCat getTr() {
		return tr;
	}

	public void setTr(TransportCat tr) {
		this.tr = tr;
	}
	
	

	public Integer getIdT() {
		return IdT;
	}

	public void setIdT(Integer idT) {
		IdT = idT;
	}

	
	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public String Ajouter()
	{
	   // upload();
		
		tr.setTransport(metiert.getTransport(IdT));
		//tr.setIconImage(file.getFileName());
		metier.addTransportCat(tr);
		return "ManageTransport.xhtml";
	}
	
	//uploading image
	 
	//**************************************************************************************************************
	public void upload() {
  
      FacesMessage msg = new FacesMessage("Success! ",file.getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file        
        try {
        	
            copyFile(file.getFileName(), file.getInputstream());
          
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }  
	
    public void copyFile(String fileName, InputStream in) {
    	
    	
    	
           try {
              
              
                // write the inputStream to a FileOutputStream
                OutputStream out = new FileOutputStream(new File(destination + fileName));
              
                int read = 0;
                byte[] bytes = new byte[1024];
              
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
              
                in.close();
                out.flush();
                out.close();
              
                System.out.println("New file created!");
                } catch (IOException e) {
                System.out.println(e.getMessage());
                }
    }

//********************************************************************************************************************	
	
	public String edit(Integer Id)
	{
		this.Id=Id;
		TransportCat c=metier.getTransportCat(Id);
		this.tr.setDescription(c.getDescription());
		this.tr.setIconImage(c.getIconImage());
		this.tr.setIntitule(c.getIntitule());
		this.tr.setStatut(c.getStatut());
    	IdT=c.getTransport().getIdTransport();
		return "EditTransport.xhtml";
		
	}
	
	public String Modifier()
	{
		TransportCat c=metier.getTransportCat(Id);
		c.setDescription(tr.getDescription());
		c.setIconImage(tr.getIconImage());
		c.setIntitule(tr.getIntitule());
		c.setStatut(tr.getStatut());
		c.setTransport(metiert.getTransport(IdT));
		metier.updateTransportCat(c);
		return "ManageTransport.xhtml";
	}

	
	public String Supprimer(Integer Id)
	{
		
		metier.deleteTransportCat(Id);
		return "succes";
		
	}
	
	
	public List<TransportCat> getListTransport()
	{
		return metier.getAllTransportCat();
	}
    
	
	
	
	public List<Transport> getListTransportt()
	{
		return metiert.getAllTransport();
	}

}
